/*
 * Copyright 2010, 2011 by the University of Richmond, Richmond, VA 23173
 * Written by Eric F. Palmer eric@ericfpalmer.com || epalmer@richmond.edu
 * And other contributors
 * http://www.richmond.edu/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Project files reside at http://code.google.com/p/files-2-exist-db
 */

package edu.richmond.is.webservices.files2exist;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;

/**
 *
 * @author epalmer
 *
 *
 * this is a class that just holds the settings for one linux folder that we wish to upload files from
 * we upload the filesin this folder 2 eXist-Db. This class can be used to construct the URL for the upload
 * as well
 *
 * for now this assumes we will want all of the files in the folder eventaully uploaded
 *
 */
public class Folder2ExistSettings {

    // String folderName - like "/WWW/users/webbatch/xmldata/bios/as" - we ingest files from this folder
    private String folderName = "";

    // String hostName - eXist-Db host name like "xmlsvc.richmond.edu" or "localhost"
    private String hostName   = "127.0.0.1";

    // Int hostPort - eXist-Db host port like "8080"
    private int hostPort = 8080;

    // String existCollection - eXist-Db path that will be appended to the end of the rest put path for eXist-Db
    //                          like /bios/as so that the URI path becomes /exist/rest/Db/bios/as
    private String existCollection = "";
    
   
    // String existCollectionPathPrefix - the rest put prefix for eXist-Db. This does not change.
    private String existCollectionPathPrefix = "/exist/rest/Db";
    
    // boolean SslExistSite - indicates if the eXist-Db instance is running httpd as ssl.
    private boolean useSsl = false;

    // String existUsername/existPassword - the optional username and password for exist put operations on this collect
    private String existUsername   = "";
    private String existPassword   = "";

    private String transformType = "";
    private String transformName = "";
    
    // Regex filter for files in directories
    private String filePattern = "";
    private boolean recurse;

	private boolean allowBinary;

    public Folder2ExistSettings(String folderName, boolean recurse, boolean allowBinary, String filter, String hostName, int hostPort, 
            String existCollection, boolean useSsl, String existUsername, String existPassword, String transformType, String transformName) {

        this.folderName      = folderName;
        this.hostName        = hostName;
        this.hostPort        = hostPort;
        this.existCollection = existCollection;
        this.useSsl          = useSsl;
        this.existUsername   = existUsername;
        this.existPassword   = existPassword;
        this.transformType   = transformType;
        this.transformName   = transformName;
        this.recurse         = recurse;
        this.filePattern     = filter;
        this.allowBinary     = allowBinary;
    }

    public String makeUri(String fileName) throws URISyntaxException, MalformedURLException {

        String protocol = "http";
        if (this.useSsl) {
            protocol = "https";
        }
        String urlFilePathFragement = this.existCollectionPathPrefix + this.existCollection + "/";

        URL url = new URL(protocol, this.hostName, this.hostPort, urlFilePathFragement);
        //System.out.println(url.toString());
        
        String uriEncodedFragement = url.toURI().toASCIIString();
        URI uri = new URI(uriEncodedFragement + fileName);
        String uriEncoded = uri.toASCIIString();
        //System.out.println(uriEncoded);
        return uriEncoded;
    }

    public String getTransformName() {
        return this.transformName;
    }

    public String getTransformType() {
        return this.transformType;
    }

    public String getExistCollection() {
        return this.existCollection;
    }

    public String getExistCollectionPathPrefix() {
        return this.existCollectionPathPrefix;
    }

    public String getExistPassword() {
        return this.existPassword;
    }

    public String getExistUsername() {
        return this.existUsername;
    }

    public String getFolderName() {
        return this.folderName;
    }

    public String getHostName() {
        return this.hostName;
    }

    public int getHostPort() {
        return this.hostPort;
    }

    public boolean isUseSsl() {
        return this.useSsl;
    }

    public boolean isRecurse() {
		return recurse;
	}

	public void setRecurse(boolean recurse) {
		this.recurse = recurse;
	}

	public String getFilePattern() {
		return filePattern;
	}

	
	public boolean isAllowBinary() {
		return allowBinary;
	}

	public void setAllowBinary(boolean allowBinary) {
		this.allowBinary = allowBinary;
	}

	public static void main(String[] args) throws URISyntaxException, MalformedURLException, UnsupportedEncodingException {

        String testStr = URLEncoder.encode("some   file name - .xml", "UTF-8");
        System.out.println(testStr);


        /* this commented out section can be used to scan a directory for file names that require encoding */
        /*
        File folder = new File("/root/Documents/downloads/biosTemp/bios/as");
        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                //System.out.println("File " + listOfFiles[i].getName());
                if (listOfFiles[i].canRead()) {
                    String encodedFileName = URLEncoder.encode(listOfFiles[i].getName(), "UTF-8");
                    if (!listOfFiles[i].getName().equals(encodedFileName)) {
                        System.out.println("***"+listOfFiles[i].getName()+"*** ***"+encodedFileName+"***");
                    }

                }
            } else if (listOfFiles[i].isDirectory()) {
                System.out.println("*****Directory " + listOfFiles[i].getName());
            }
        }

        */

        //Folder2ExistSettings testFolder = new Folder2ExistSettings("/somefolder/bios/as", "xmlsvc.richmond.edu", 8080, "/people/bios/as", false);
        //String uriString = testFolder.makeUri("some file name .xml");
    }

}